package com.company.cloud.page.modules.app.controller;

import com.company.cloud.behavior.modules.api.response.BehaviorSearchParam;
import com.company.cloud.behavior.modules.api.response.ThumbsUpSearchModel;
import com.company.cloud.behavior.modules.api.service.BehaviorClientService;
import com.company.cloud.page.modules.api.service.PageClientService;
import com.company.common.server.constant.IRelBusiType;
import com.company.common.server.log.LogSaasUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.page.constant.PageCardTypeEnum;
import com.company.cloud.page.modules.app.controller.req.PageDataQuery4PageParam;
import com.company.cloud.page.modules.app.service.PageDataService;
import com.company.cloud.page.modules.base.entity.PageCardConfigInfo;
import com.company.cloud.page.modules.base.service.PageCardConfigInfoService;
import com.company.cloud.page.modules.cache.models.PageCardModel;
import com.company.cloud.page.modules.cache.service.PageCardCacheService;
import com.company.cloud.page.modules.cache.utils.PageCachekeys;
import com.company.cloud.page.modules.cache.utils.PageConvertUtils;
import com.company.cloud.page.modules.mgr.controller.req.PageCardSaveParam;
import com.company.cloud.page.modules.mgr.service.PageCardConfigMgrService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/app/cms/pageCard")
@Api(tags = "Page_App_名片(数据)信息")
public class PageCardController {

	@Autowired
	private PageCardCacheService pageCardCacheService;
	@Autowired
	private PageDataService pageDataService;
	@Autowired
	private RedisUtils redisUtils;
	@Autowired
	private PageCardConfigMgrService pageCardConfigMgrService;
	@Autowired
	private PageCardConfigInfoService pageCardConfigInfoService;
//	@Autowired
//	private UbaLogRemoteService ubaLogRemoteService;

	
	@GetMapping("/getPageCardByNo")
	@ApiOperation(value = "名片信息(不含内容)", response = PageCardModel.class)
	public BaseResult getPageCardByNo(@RequestParam("pageNo") String pageNo) {
		if (StringUtils.isEmpty(pageNo)) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		PageCardModel data = pageCardCacheService.getPageCardConfigCache(pageNo);
		//todo:临时方案
//		LogSaasUtils.saveUserBrowseObjLogOne(ubaLogRemoteService, IRelBusiType.CMS_PageCard,data.getPageNo());
		return BaseResult.ok(data);
	}

	@GetMapping("/getPageCard2DataByNo")
	@ApiOperation(value = "名片信息和数据", response = PageCardModel.class)
	public BaseResult getPageCard2DataByNo(@RequestParam("pageNo") String pageNo) {
		PageCardModel data =this._page2Data(pageNo);
		//todo:临时方案
//		LogSaasUtils.saveUserBrowseObjLogOne(ubaLogRemoteService,IRelBusiType.CMS_PageCard,pageNo);
		return BaseResult.ok(data);
	}

	private PageCardModel _page2Data(String pageNo){
		if (StringUtils.isEmpty(pageNo)) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUserNoValidate();
		PageCardModel data = pageCardCacheService.getPageCardConfig2DataByCache(pageNo,userInfo);
		if(data!=null) {
			data.setContent(null);
		}
		return data;
	}
	
	@Login
	@GetMapping("/getPageCard2DataByUser")
	@ApiOperation(value = "获取当前用户的名片信息和数据", response = PageCardModel.class)
	public BaseResult getPageCard2DataByUser() {
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		PageCardConfigInfo entity = pageCardConfigMgrService.getConfigByUser(userInfo);
		if (entity != null) {
			PageCardModel data=this._page2Data(entity.getPageNo());
//			LogSaasUtils.saveUserBrowseObjLogOne(ubaLogRemoteService,IRelBusiType.CMS_PageCard,entity.getPageNo());
			return BaseResult.ok(data);
		}
		return BaseResult.ok();
	}
	
	@Login
	@PostMapping("/save2updateByUser")
	@ApiOperation(value = "新增或修改当前用户的名片信息", response = BaseResult.class)
	public BaseResult save2updateByUser(@RequestBody PageCardSaveParam params) {
		ValidatorUtils.validate(params);
		if(!PageCardTypeEnum.CardType_Cust.getCode().equals(params.getPageType())) {
			throw new BusinessException(BaseReturnEnum.PARAM_ERROR);
		}
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		PageCardConfigInfo entity =pageCardConfigMgrService.addOrUpdate(params,userInfo);
		pageCardCacheService.removePageCardConfigCache(entity.getPageNo());
		return BaseResult.ok();
	}

	@PostMapping("/queryPageCardData")
	@ApiOperation(value = "名片数据查询", response = BaseResult.class)
	public BaseResult queryPageCardData(@RequestBody PageDataQuery4PageParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUserNoValidate();
		Object data = pageDataService.queryPageEleData(params, userInfo);
//		LogSaasUtils.saveUserBrowseObjLogOne(ubaLogRemoteService,IRelBusiType.CMS_PageCard,params.getPageNo());
		return BaseResult.ok(data);
	}
	
	

	
	
	

	
}
